package com.pz.gulimall.product.schdule;

import com.pz.gulimall.product.entity.SpuInfoEntity;
import com.pz.gulimall.product.service.SpuInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * sku 定时任务
 */

@Slf4j
@Component
public class SpSchedule {
    @Autowired
    private SpuInfoService spuInfoService;

    /**
     * 定时将 商品信息同步到ES 每周周六 0:00 执行
     */
    @Scheduled(cron = "0 0 0 ? * WED")
    @Async
    public void spuUpdateToEs(){
        // 获取上架状态为 1 的所有 spuId
        List<Long> publishSpuIds = spuInfoService.queryAllPublish().stream().map(SpuInfoEntity::getId).collect(Collectors.toList());

        //调用 上架接口 更新数据
        log.info("更新搜索引擎中的数据-- spuIds: [{}]", publishSpuIds);

        publishSpuIds.forEach(spuId -> {
            spuInfoService.upSpu(spuId);
        });
    }
}
